*** REPLICATION FILES Dietrich, Milner and Slapin “From Text to Political Positions on Foreign Aid: Analysis of Aid Mentions in Party Manifestos from 1960 to 2015”
*** File 2 
*** 29 May 2020
*** JBS
*** Stata SE 15.1 

*** THIRD FILE TO RUN
*** LOADS output of File 2
*** Runs Main Models in Paper
*** If you wish to print out summary stats, you must install "asdoc": ssc install asdoc, update


use output/File2output.dta  , clear

xtset ccode year

gen decade = 1 if year<1970
replace decade = 2 if year>=1970 & year<1980
replace decade = 3 if year>=1980 & year<1990
replace decade = 4 if year>=1990 & year<2000
replace decade = 5 if year>=2000 & year<2010
replace decade = 6 if year>=2010 & year~=.

gen hdec = 1 if year>=1990 & year<1995
replace hdec = 2 if year>=1995 & year<2000
replace hdec = 3 if year>=2000 & year<2005
replace hdec = 4 if year>=2005 & year<2010
replace hdec = 5 if year>=2010 & year<2015

gen loggdppc = log(gdppc_k)
gen logimmig_in = log(immig_in)

gen coldwar = 0
replace coldwar = 1 if year >= 1992

sort year

*** Make GDP in Billions
gen gdp1 = gdp/1000000000

preserve

*** Main Models for Table 1 in Main Paper
*** 8 Models
*** PM aid mentions (overall and Pro) commit controlling for GDP, Lagged Commit, Commit/GDP, Commit/Pop

estimates clear  
local i = 0
tsset ccode year

xtreg usd_commitment c.meanaid_totpm##c.meanleft_rightseats U_natl  c.gdp1 dgdp loggdppc tincident logimmig_in globalaidtot logdd NumPartyCab i.hdec , fe cl(ccode) 
*asdoc summarize usd_commitment commit_gdp commit_pop meanaid_totpm meanproaid_totpm meanaid_totdevt meanproaid_totdevt meanaid_totseats meanproaid_totseats meanleft_rightseats U_natl gdp dgdp loggdppc tincident logimmig_in aid_tot logdd NumPartyCab if e(sample), dec(2) save(output/sumstats.doc) replace
summarize usd_commitment commit_gdp commit_pop meanaid_totpm meanproaid_totpm meanaid_totdevt meanproaid_totdevt meanaid_totseats meanproaid_totseats meanleft_rightseats U_natl gdp dgdp loggdppc tincident logimmig_in aid_tot logdd NumPartyCab if e(sample)

est store tab_1m1
margins, at(meanaid_totpm=(1 1.6725) meanleft_rightseats=(1 5)) post
noi nlcom 	(_b[3._at]-_b[1._at]) ///
			(_b[4._at]-_b[2._at]) ///
			((_b[4._at]-_b[2._at]) - (_b[3._at]-_b[1._at]))

xtreg usd_commitment c.meanproaid_totpm##c.meanleft_rightseats U_natl c.gdp1 dgdp loggdppc tincident logimmig_in globalaidtot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_1m2
margins, at(meanproaid_totpm=(1 1.5738) meanleft_rightseats=(1 5)) post
noi nlcom 	(_b[3._at]-_b[1._at]) ///
			(_b[4._at]-_b[2._at]) ///
			((_b[4._at]-_b[2._at]) - (_b[3._at]-_b[1._at]))

xtreg usd_commitment L.usd_commitment c.meanaid_totpm##c.meanleft_rightseats U_natl  c.gdp1 dgdp loggdppc tincident logimmig_in globalaidtot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_1m3

xtreg usd_commitment L.usd_commitment c.meanproaid_totpm##c.meanleft_rightseats U_natl  c.gdp1 dgdp loggdppc tincident logimmig_in globalaidtot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_1m4

xtreg commit_gdp c.meanaid_totpm##c.meanleft_rightseats U_natl dgdp loggdppc tincident logimmig_in globalaidtot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_1m5

xtreg commit_gdp c.meanproaid_totpm##c.meanleft_rightseats U_natl dgdp loggdppc tincident logimmig_in globalaidtot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_1m6

xtreg commit_pop c.meanaid_totpm##c.meanleft_rightseats U_natl c.gdp1 dgdp loggdppc tincident logimmig_in globalaidtot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_1m7

xtreg commit_pop c.meanproaid_totpm##c.meanleft_rightseats U_natl c.gdp1 dgdp loggdppc tincident logimmig_in globalaidtot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_1m8

* Output Table 
#delim ;
estout tab_1m1 tab_1m2 tab_1m3 tab_1m4 tab_1m5 tab_1m6 tab_1m7 tab_1m8 using output/tab_1M.txt , starlevels(* .1 ** .05 *** .01) cells(b(star fmt(%9.3f)) se(par fmt(%9.3f)))
  stats(N N_clust r2  , fmt( %9.0f %9.0f %9.3f %9.3f) labels(N Clusters R2 )) label replace  ;
#delim cr

*************************
** Robustness Checks For Appendix D
*************************

xtreg usd_commitment c.meanproaid_totpm##c.meanleft_rightseats U_natl  c.gdp1 dgdp loggdppc tincident logimmig_in aid_tot logdd i.hdec if NumPartyCab > 1, fe cl(ccode) 
est store tab_A1m1

xtreg usd_commitment c.meanaid_totdevt##c.meanleft_rightseats U_natl c.gdp1 dgdp loggdppc tincident logimmig_in aid_tot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_A1m2

xtreg usd_commitment c.meanproaid_totdevt##c.meanleft_rightseats U_natl c.gdp1 dgdp loggdppc tincident logimmig_in aid_tot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_A1m3

xtreg usd_commitment c.meanaid_totseats##c.meanleft_rightseats U_natl  c.gdp1 dgdp loggdppc tincident logimmig_in aid_tot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_A1m4

xtreg usd_commitment c.meanproaid_totseats##c.meanleft_rightseats U_natl  c.gdp1 dgdp loggdppc tincident logimmig_in aid_tot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_A1m5

xtreg usd_commitment c.meanproaid_totpm##c.meanstate_marketseats U_natl  c.gdp1 dgdp loggdppc tincident logimmig_in aid_tot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_A1m6

xtreg usd_commitment c.meanproaid_totpm##c.meanliberty_authorityseats U_natl  c.gdp1 dgdp loggdppc tincident logimmig_in aid_tot logdd NumPartyCab i.hdec , fe cl(ccode) 
est store tab_A1m7


*output table
#delim ;
estout tab_A1m1 tab_A1m2 tab_A1m3 tab_A1m4 tab_A1m5 tab_A1m6 tab_A1m7 using output/tab_APModels.txt , starlevels(* .1 ** .05 *** .01) cells(b(star fmt(%9.3f)) se(par fmt(%9.3f)))
  stats(N N_clust r2  , fmt( %9.0f %9.0f %9.3f %9.3f) labels(N Clusters R2 )) label replace  ;
#delim cr


************************************************
* marginal effects plots from main model
* FIGURE 2 in paper
************************************************

set obs 10000
xtreg usd_commitment c.meanaid_totpm##c.meanleft_rightseats U_natl  c.gdp1 dgdp loggdppc tincident logimmig_in aid_tot logdd NumPartyCab i.hdec , fe cl(ccode) 
display r(b)

matrix b=e(b)
matrix V=e(V)

*coef on meanaid_totpm
scalar b1=b[1,1]
*coef on interaction
scalar b3=b[1,3]

scalar varb1=V[1,1]
scalar varb3=V[3,3]

scalar covb1b3=V[1,3]

scalar list b1 b3 varb1 varb3 covb1b3

*     ****************************************************************  *;
*       Calculate data necessary for top marginal effect plot.          *;
*     ****************************************************************  *;

generate MVZ=_n/100 + 0.99

replace  MVZ=. if MVZ>5

gen conbx=b1+b3*MVZ if MVZ<5

gen consx=sqrt(varb1+varb3*(MVZ^2)+2*covb1b3*MVZ) if MVZ<5

gen ax=1.96*consx

gen upperx=conbx+ax

gen lowerx=conbx-ax

*     ****************************************************************  *;
*       Construct stuff to produce the rug plot.  Need to create an     *;
*       offset position for the pipe marker, which will depend on       *;
*       where the histogram sits on the y-axis. This will requie some   *;
*       trial and error.                                                *;
*     ****************************************************************  *;

gen where= 100

gen pipe = "|"

egen tag_meanleft_rightseats = tag(meanleft_rightseats)

gen trunc_meanleft_rightseats = meanleft_rightseats

replace trunc_meanleft_rightseats =. if meanleft_rightseats > 5 
*     ****************************************************************  *;
*       Construct variable to produce y=0 line.                         *;
*     ****************************************************************  *;

gen yline=0

*     ****************************************************************  *;
*     ****************************************************************  *;
*       Produce marginal effect plot for X.                             *;
*     ****************************************************************  *;
*     ****************************************************************  *;

graph set window fontface "Times New Roman"

graph twoway scatter where trunc_meanleft_rightseats if tag_meanleft_rightseats, plotr(m(b 5)) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off) ///
	    ||   line conbx   MVZ, clpattern(solid) clwidth(medium) clcolor(black) yscale(r(-0.01 0.01)) ///
        ||   line upperx  MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
        ||   line lowerx  MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
        ||   line yline  MVZ,  clwidth(thin) clcolor(black) clpattern(solid)  ///
					xlabel(1 2 3 4 5, nogrid labsize(3)) ///
					ylabel(-2000 -1000  0 1000 2000, nogrid labsize(3)) ///
					xtitle("Left-Right Government Position""Seat Weighted Average") ///
                    ytitle("Marginal Effect of One Additional Aid Mention" "on Aid Commitments (USD, Millions)") ///
                    xsca(titlegap(2)) ///
				  	ysca(titlegap(2)) ///
					title("Aid Mentions (Total)" "Per 1000 Words") ///
					scheme(s1mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
					name(figmargin1, replace)

restore
					
set obs 10000
xtreg usd_commitment c.meanproaid_totpm##c.meanleft_rightseats U_natl  c.gdp1 dgdp loggdppc tincident logimmig_in aid_tot logdd NumPartyCab i.hdec , fe cl(ccode) 

matrix b=e(b)
matrix V=e(V)

scalar b1=b[1,1]
scalar b3=b[1,3]

scalar varb1=V[1,1]
scalar varb3=V[3,3]

scalar covb1b3=V[1,3]

scalar list b1 b3 varb1 varb3 covb1b3

*     ****************************************************************  *;
*       Calculate data necessary for top marginal effect plot.          *;
*     ****************************************************************  *;

generate MVZ=_n/100 + 0.99

replace  MVZ=. if MVZ>5

gen conbx=b1+b3*MVZ if MVZ<5

gen consx=sqrt(varb1+varb3*(MVZ^2)+2*covb1b3*MVZ) if MVZ<5

gen ax=1.96*consx

gen upperx=conbx+ax

gen lowerx=conbx-ax

*     ****************************************************************  *;
*       Construct stuff to produce the rug plot.  Need to create an     *;
*       offset position for the pipe marker, which will depend on       *;
*       where the histogram sits on the y-axis. This will requie some   *;
*       trial and error.                                                *;
*     ****************************************************************  *;

gen where= 100

gen pipe = "|"

egen tag_meanleft_rightseats = tag(meanleft_rightseats)

gen trunc_meanleft_rightseats = meanleft_rightseats

replace trunc_meanleft_rightseats =. if meanleft_rightseats > 5 
*     ****************************************************************  *;
*       Construct variable to produce y=0 line.                         *;
*     ****************************************************************  *;

gen yline=0

*     ****************************************************************  *;
*     ****************************************************************  *;
*       Produce marginal effect plot for X.                             *;
*     ****************************************************************  *;
*     ****************************************************************  *;

graph set window fontface "Times New Roman"

graph twoway scatter where trunc_meanleft_rightseats if tag_meanleft_rightseats, plotr(m(b 5)) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off) ///
	    ||   line conbx   MVZ, clpattern(solid) clwidth(medium) clcolor(black) yscale(r(-0.01 0.01)) ///
        ||   line upperx  MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
        ||   line lowerx  MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
        ||   line yline  MVZ,  clwidth(thin) clcolor(black) clpattern(solid)  ///
					xlabel(1 2 3 4 5, nogrid labsize(3)) ///
					ylabel(-2000 -1000  0 1000 2000, nogrid labsize(3)) ///
					xtitle("Left-Right Government Position""Seat Weighted Average") ///
                    ytitle("") ///
                    xsca(titlegap(2)) ///
				  	ysca(titlegap(2)) ///
					title("Aid Mentions (Positive)" "Per 1000 Words" ) ///
					scheme(s1mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
					name(figmargin2, replace)

graph combine figmargin1 figmargin2 , title("Marginal Effects of Aid Mentions on Aid Commitments""by Government Ideology") scheme(s1mono) 

graph export output/marginaleffectsplot.pdf, replace

******************************************
*** Additional model controlling for opposition party aid mentions
xtreg usd_commitment c.meanaid_totpm##c.meanleft_rightseats meanaid1o U_natl  c.gdp1 dgdp loggdppc tincident logimmig_in globalaidtot logdd NumPartyCab i.hdec , fe cl(ccode) 

